package oer.halk.intercept_demo.filter;


import oer.halk.intercept_demo.intercept.BodyReaderWrapper;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 将请求的body参数存起来
 *
 * @Author zhuhao
 * @Date 2022/1/26 17:01
 * @desc
 */
public class BodyReaderFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        BodyReaderWrapper bodyReaderWrapper = null;
        if (servletRequest instanceof HttpServletRequest) {
            bodyReaderWrapper = new BodyReaderWrapper((HttpServletRequest) servletRequest);
        }

        if (bodyReaderWrapper == null) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            filterChain.doFilter(bodyReaderWrapper, servletResponse);
        }

    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
